Skip to content

feat: adds AI agent detection to auto-switch to JSON output#4938

Closed
pushpak1300 wants to merge 29 commits intophpstan:2.1.xfrom
pushpak1300:2.2.x
Closed

feat: adds AI agent detection to auto-switch to JSON output#4938
pushpak1300 wants to merge 29 commits intophpstan:2.1.xfrom
pushpak1300:2.2.x

Conversation

@pushpak1300
Copy link

@pushpak1300 pushpak1300 commented Feb 15, 2026

This pull request adds AI agent detection that automatically switches PHPStan to JSON error format when executed within Claude Code, Cursor, Gemini CLI, Codex, OpenCode, Augment, Replit, or Devin.

Why:

The default PHPStan output is designed for humans - colorful tables, progress bars with animations, and formatted summaries. AI agents need something different: structured JSON they can reliably parse, unambiguous error details with file paths and line numbers, minimal output that saves context window and reduces token cost, and no progress bar animations that waste tokens on redraw noise.

How it works:

  1. When Claude Code runs PHPStan, it sets the CLAUDECODE / CLAUDE_CODE environment variable
  2. When Cursor runs PHPStan, it sets CURSOR_TRACE_ID / CURSOR_AGENT
  3. Other agents set their own env vars (GEMINI_CLI, CODEX_SANDBOX, AI_AGENT, etc.)
  4. PHPStan detects these via shipfastlabs/agent-detector and automatically switches to JSON format + suppresses progress bar animations - no flags needed

Output:

{"totals":{"errors":0,"file_errors":1},"files":{"/path/to/file.php":{"errors":1,"messages":[{"message":"Error message","line":10,"ignorable":true,"identifier":"error.id"}]}},"errors":[]}

Explicit --error-format=table or config errorFormat: table always takes priority over agent detection.

related laravel/pint#415

ondrejmirtes and others added 29 commits February 13, 2026 10:49
Automatically detects undocumented parameters in conf/parametersSchema.neon
and creates a draft PR on phpstan/phpstan with documentation updates.
Triggers on push to 2.2.x when the schema changes, or manually.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Use github.event.before for push diffs to handle multi-commit pushes.
Add explicit else branch for manual dispatch to check entire schema.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
These parameters exist purely to be toggled by rule levels
and are not configured by users directly.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The safe-output patch was trying to create the file as new, but it
already exists in phpstan/phpstan. Add a pre-step that fetches and
commits the file so the agent's edits produce a modification patch.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Instead of using safe-outputs create-pull-request (which generates
patches that fail on cross-repo applies), checkout phpstan/phpstan
to __phpstan-website/ subdirectory, edit config-reference.md in place,
and push branch + create PR via bash/gh CLI.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The gh-aw framework runs git commands in the workspace root expecting
a git repository. Without checking out phpstan-src first, the
"Configure Git credentials" step fails with "not a git repository".

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The parametersSchema.neon has nested structure() blocks like
exceptions.check.* and cache.*. The agent was only looking at
top-level parameters and missing nested ones like throwTypeCovariance
and tooWideImplicitThrowType.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@phpstan-bot
Copy link
Collaborator

You've opened the pull request against the latest branch 2.2.x. PHPStan 2.2 is not going to be released for months. If your code is relevant on 2.1.x and you want it to be released sooner, please rebase your pull request and change its target to 2.1.x.

@pushpak1300 pushpak1300 changed the base branch from 2.2.x to 2.1.x February 15, 2026 12:51
@pushpak1300
Copy link
Author

Closing in favor of a new PR targeting 2.1.x with the correct head branch.

pushpak1300 added a commit to pushpak1300/phpstan-src that referenced this pull request Feb 15, 2026
Adds helgesverre/toon and creates ToonErrorFormatter that outputs in
TOON (Token-Oriented Object Notation) format. When an AI agent is
detected, PHPStan now outputs in TOON instead of JSON, reducing token
consumption while remaining machine-parseable.

Follows up on phpstan#4938.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants